/// <reference path="brandIE.controls.js" />

(function () {
    // publish to namespacebase 'nsbase.controls.eventObserver'
    nsbase.controls.eventObserver = EventObserver;

    function EventObserver(eventContext) {
        var _self = this;
        var _eventHolder = {},
            _eventContext = eventContext;

        _self.on = function (eventName, callBackFunc) {
            if (_eventHolder[eventName] == null) {
                _eventHolder[eventName] = [];
            }
            _eventHolder[eventName].push(callBackFunc);
        };

        _self.fireEvent = function (eventName, eventParams) {
            var callBackFuncs = _eventHolder[eventName];
            if (callBackFuncs && callBackFuncs.length > 0) {
                for (var i = 0; i < callBackFuncs.length; i++) {
                    eventParams = eventParams || [];
                    callBackFuncs[i].apply(_eventContext, eventParams);
                }
            }
        };

        _self.un = function (eventName) {
            if (_eventHolder[eventName])
                _eventHolder[eventName] = [];
        };
        _self.unAll = function () {
            for (var name in _eventHolder) {
                _eventHolder[name] = [];
            }
        }
    };
})();